import { Order } from '../order/order-model.js';
import { Cart } from '../cart/cart-model.js';
import { Address } from '../../utils/address.js';

var order = new Order();
var cart = new Cart();
var address = new Address();

Page({
	data: {
		fromCartFlag: true,
		addressInfo: null,
		remarks: null,
		kd_index: 0
	},

	/*
	*绑定remarks字符
	**/
	bindTextAreaBlur: function (e) {
		this.data.remarks = e.detail.value;
		console.log(this.data.remarks);
	},

	/*
	* 订单数据来源包括两个：
	* 1.购物车下单
	* 2.旧的订单
	* */
	onLoad: function (options) {
		var flag = options.from == 'cart',
			that = this;
		this.data.fromCartFlag = flag;
		this.data.account = options.account;
		this.data.weightCount = options.weight_count;

		//来自于购物车
		if (flag) {
			var account = parseFloat(options.account);
			var kdCost = 6;
			var weight_count = parseFloat(options.weight_count);
			var referral_code = null;
			if (wx.getStorageSync('referral_code_by_other')) {
				referral_code = wx.getStorageSync('referral_code_by_other');
			}
			if (wx.getStorageSync('referral_code')) {
				referral_code = wx.getStorageSync('referral_code');
			}
			if (weight_count > 1) {
				kdCost += (Math.ceil(weight_count - 1)) * 4;
			}
			this.setData({
				productsArr: cart.getCartDataFromLocal(true),
				orderStatus: 0,
				account: account + kdCost,
				kdPicker: ['百世汇通-费用' + kdCost + '元'],
				weightCount: options.weight_count,
				referral_code: referral_code
			});
			/*显示收获地址*/
			address.getAddress((res) => {
				that._bindAddressInfo(res);
			});
		}

		//旧订单
		else {
			this.data.id = options.id;
		}
	},

	onShow: function () {
		if (this.data.id) {
			var that = this;
			//下单后，支付成功或者失败后，点左上角返回时能够更新订单状态 所以放在onshow中
			var id = this.data.id;
			order.getOrderInfoById(id, (data) => {
				that.setData({
					orderStatus: data.status,
					productsArr: data.snap_items,
					account: data.total_price,
					remarks: data.remarks,
					referral_code: data.referral_code,
					kdPicker: ['百世汇通-费用' + data.kdcost + '元'],
					weightCount: data.weight,
					basicInfo: {
						orderTime: data.create_time,
						orderNo: data.order_no
					},
					com:data.lc,
					nu:data.sc,
				});

				// 快照地址
				var addressInfo = data.snap_address;
				addressInfo.totalDetail = address.setAddressInfo(addressInfo);
				that._bindAddressInfo(addressInfo);
			});
		}
	},

	setReferralCode: function (e) {
		this.setData({
			referral_code: e.detail.value
		})
	},

	/*修改或者添加地址信息*/
	editAddress: function () {
		var that = this;
		wx.getSetting({//先获取用户当前的设置  
			success(res) {
				if (!res.authSetting['scope.address']) {
					wx.authorize({
						scope: 'scope.address',
						success(res) {
							that.chooseWxAddress()
						},
						fail(res) {
							wx.showModal({
								title: '操作提示',
								content: '请授权使用微信通讯地址,点击确认开启授权',
								showCancel: true,
								success: function (res) {
									console.log(res)
									if (res.confirm == true) {
										wx.openSetting({
										})
									}
								}
							});
						}
					})
				} else {
					that.chooseWxAddress()
				}
			}
		})
	},

	chooseWxAddress: function () {
		var that = this;
		wx.chooseAddress({
			success: function (res) {
				var addressInfo = {
					name: res.userName,
					mobile: res.telNumber,
					totalDetail: address.setAddressInfo(res)
				};
				that._bindAddressInfo(addressInfo);
				console.log(addressInfo);

				//保存地址
				address.submitAddress(res, (flag) => {
					if (!flag) {
						that.showTips('操作提示', '地址信息更新失败！');
					}
				});
			}
		})
	},

	/*绑定地址信息*/
	_bindAddressInfo: function (addressInfo) {
		this.setData({
			addressInfo: addressInfo
		});
	},

	/*下单和付款*/
	pay: function () {
		if (!this.data.addressInfo) {
			this.showTips('下单提示', '请填写您的收货地址');
			return;
		}
		if (this.data.orderStatus == 0) {
			this._firstTimePay();
		} else {
			this._oneMoresTimePay();
		}
	},

	/*第一次支付*/
	_firstTimePay: function () {
		if (this.data.referral_code) {
			if (this.data.referral_code.length != 6) {
				this.showTips("提示", "输入6位推荐码", false);
				exit;
			}
		}
		var orderInfo = [],
			procuctInfo = this.data.productsArr,
			order = new Order();
		for (let i = 0; i < procuctInfo.length; i++) {
			orderInfo.push({
				product_id: procuctInfo[i].id,
				count: procuctInfo[i].counts
			});
		}

		var that = this;
		//支付分两步，第一步是生成订单号，然后根据订单号支付
		order.doOrder(orderInfo, that.data.remarks, that.data.referral_code, (data) => {
			//订单生成成功
			if (data.pass) {
				//更新订单状态
				var id = data.order_id;
				that.data.id = id;
				that.data.fromCartFlag = false;

				//开始支付
				that._execPay(id);
			} else {
				that._orderFail(data);  // 下单失败
			}
		});
	},

	/*
	*改变快递状态
	*/
	PickerKdChange: function (e) {
		console.log(e)
		this.setData({
			kd_index: e.detail.value
		})
	},

	/*
	* 提示窗口
	* params:
	* title - {string}标题
	* content - {string}内容
	* flag - {bool}是否跳转到 "我的页面"
	*/
	showTips: function (title, content, flag) {
		wx.showModal({
			title: title,
			content: content,
			showCancel: false,
			success: function (res) {
				if (flag) {
					wx.switchTab({
						url: '/pages/my/my'
					});
				}
			}
		});
	},

	/*
	*下单失败
	* params:
	* data - {obj} 订单结果信息
	* */
	_orderFail: function (data) {
		var nameArr = [],
			name = '',
			str = '',
			pArr = data.pStatusArray;
		for (let i = 0; i < pArr.length; i++) {
			if (!pArr[i].haveStock) {
				name = pArr[i].name;
				if (name.length > 15) {
					name = name.substr(0, 12) + '...';
				}
				nameArr.push(name);
				if (nameArr.length >= 2) {
					break;
				}
			}
		}
		str += nameArr.join('、');
		if (nameArr.length > 2) {
			str += ' 等';
		}
		str += ' 缺货';
		wx.showModal({
			title: '下单失败',
			content: str,
			showCancel: false,
			success: function (res) {

			}
		});
	},

	/* 再次次支付*/
	_oneMoresTimePay: function () {
		this._execPay(this.data.id);
	},

	/*
	*开始支付
	* params:
	* id - {int}订单id
	*/
	_execPay: function (id) {
		if (!order.onPay) {
			this.showTips('支付提示', '本产品仅用于演示，支付系统已屏蔽', true);//屏蔽支付，提示
			this.deleteProducts(); //将已经下单的商品从购物车删除
			return;
		}
		var that = this;
		order.execPay(id, (statusCode) => {
			if (statusCode != 0) {
				that.deleteProducts(); //将已经下单的商品从购物车删除   当状态为0时，表示

				var flag = statusCode == 2;
				wx.navigateTo({
					url: '../pay-result/pay-result?id=' + id + '&flag=' + flag + '&from=order'
				});
			}
		});
	},

	//将已经下单的商品从购物车删除
	deleteProducts: function () {
		var ids = [], arr = this.data.productsArr;
		for (let i = 0; i < arr.length; i++) {
			ids.push(arr[i].id);
		}
		cart.delete(ids);
	},
	kd:function(){
		var com = this.data.com;
		var nu = this.data.nu;
		wx.navigateTo({
			url: '../kd/kd?com='+com+'&nu='+nu
		})
	},
	//分享效果
	onShareAppMessage: function () {
		var referral_code = wx.getStorageSync('referral_code');
		return {
			title: '唐潮盐匠-自贡冷吃系列',
			path: 'pages/home/home?referral_code=' + referral_code
		}
	}


}
)